<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- 注意：label决定当前选中的值 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curEmployeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.LodeRoles()
  },
  methods: {
    // 关闭对话框
    closeDialog() {
      this.$emit('close')
    },
    // 获取所有权限
    async LodeRoles() {
      try {
        const res = await getRoles()
        this.list = res.data.rows
        this.getUserDetailByIdww()
        // 保存数据
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    // 获取当前角色所有权限
    async getUserDetailByIdww() {
      try {
        const res = await getUserDetailById(this.curEmployeId)
        console.log(res.data)
        this.roleIds = res.data.roleIds
      } catch (err) {
        console.log('getUserDetailById', err)
      }
    },
    // 保存当前角色信息
    async hSubmit() {
      const res = await assignRoles({ id: this.curEmployeId, roleIds: this.roleIds })
      console.log('保存角色', res)
      this.$emit('close')
    }
  }
}
</script>
